Node.js와 연동 시 Authentication method 에러 발생

✒️ 2025-05-28 16:04 내용 수정



2-1. Node.js에서 mysql 라이브러리 대신 mysql2 설치

  1. VSC 터미널에서 npm un mysql로 설치 되어있던 mysql을 먼저 삭제한다.
  2. npm i mysql2으로 mysql2를 설치한 후 다시 DB와 연결을 시도한다.

mysql_authentication_error 3.png


2-2. Node.js에서 연동 시 MySQL의 포트 번호 추가

  1. 서버 설정 js 파일에서 connection 객체에 port property를 추가한다.
const mysql = require('mysql2');
const connection = mysql.createConnection({
    host : 'localhost',
    user : 'root',
    password : '비밀번호',
    database : 'DB이름',
    port : '3306' // port 번호 추가
});

2-3. Authentication Method 변경

1. MySQL Workbench에서 쿼리문 실행

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '비밀번호';

-- 위 쿼리문 실행 후 적용하려면 반드시 flush를 해야 함
FLUSH privileges;

SELECT Host, User, plugin, authentication_string FROM mysql.user;

mysql_authentication_error 4.png

2. MySQL Installer에서 Authentication Method 변경

mysql_authentication_error 6.png

mysql_authentication_error 7.png

mysql_authentication_error 8.png

mysql_authentication_error 9.png

mysql_authentication_error 10.png

mysql_authentication_error 11.png

3. MySQL의 my.ini 파일에 구문 추가

mysql_authentication_error 13.png

mysql_authentication_error 14.png

mysql_authentication_error 15.png

mysql_authentication_error 16.png


2-4. MySQL 8.0.33 버전으로 재설치